package com.dragon.client.controller.supplies;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.AddPage;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.supplies.IProcurementService;
import com.dragon.repository.domain.dto.ProcurementDTO;
import com.dragon.repository.domain.vo.ProcurementVO;
import com.dragon.repository.entity.Procurement;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Charlotte
 */
@RestController
@RequestMapping("cancelReturnable")
@Api(tags = "退货")
public class CancelReturnableController {

    @Autowired
    IProcurementService service;

    @GetMapping("list")
    @ApiOperation(value = "退货列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "id", value = "详情id", required = false, type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索参数（标题）", required = false, type = "string"),
            @ApiImplicitParam(name = "state", value = "审核状态", required = false, type = "int")
    })
    public ResponseData<List<ProcurementDTO>> listCancelReturnable(@RequestParam Integer page,
                                                                   @RequestParam Integer limit,
                                                                   @RequestParam(required = false) Integer id,
                                                                   @RequestParam(required = false) String search,
                                                                   @RequestParam(required = false) Integer state){
        Page<Procurement> procurementPage = service.listProcurement(page, limit, id, search, state, 2);
        return ResponseData.success(ProcurementDTO.convertList(procurementPage.getRecords()), procurementPage.getTotal());
    }

    @PostMapping("add")
    @ApiOperation(value = "添加")
    @AddPage
    public ResponseData addPurchase(@RequestBody @Validated(Insert.class) ProcurementVO vo){
        return ResponseData.success(service.addCancel(ProcurementVO.convert(vo)));
    }

    @PostMapping("cancel")
    @ApiOperation(value = "取消退货")
    public ResponseData cancelReturnable(@ApiParam(name = "id", value = "主表id", required = true, type = "int")
                                         @RequestParam Integer id){
        service.cancel(id);
        return ResponseData.success();
    }

}
